/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyWidget;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author KF-NRS
 */
public class FrontFont
{
    private String path = "ImageFolder/minidy.ttf";
    private String color = "0x0099FF";
    private Color thecolor = Color.decode(color);
    private Font frontfont;
    private int size = 22;

    public Color getColor()
    {
        return thecolor;
    }

    public String getColorHexString()
    {
        return color;
    }

    public void setColor(String color)
    {
        if(color!=null&&!color.trim().equals(""))
        {
            try
            {
                this.color = color;
                thecolor = Color.decode(this.color);
            }
            catch(java.lang.NumberFormatException e)
            {
                this.color="0x0099FF";
                thecolor = Color.decode(this.color);
            }
        }
    }

    public String getPath()
    {
        return path;
    }

    public void setPath(String path)
    {
        if(path!=null&&!path.trim().equals("")&&!path.equals(this.path))
        {
            this.path = path;
            initFontPath();
        }
    }

    public void setFontSize(int size)
    {
        if(size>0&&size!=this.size)
        {
            this.size = size;
            initFontSize();
        }
    }

    public int getFontSize()
    {
        return size;
    }

    private void initFontPath()
    {
        InputStream is = null;
        try
        {
            //is = new FileInputStream(path);
        	is = this.getClass().getResourceAsStream(this.path);
            frontfont = Font.createFont(Font.TRUETYPE_FONT,is).deriveFont((float)size);
        }
        catch(FontFormatException ex)
        {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(is!=null)
            {
                try
                {
                    is.close();
                }
                catch(IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

    private void initFontSize()
    {
        if(frontfont!=null)
        {
            if(size!=frontfont.getSize())
            {
                frontfont = frontfont.deriveFont((float)size);
            }
        }
        else
        {
            initFontPath();
        }
    }

    public Font getFont()
    {
        if(frontfont==null)
        {
            initFontPath();
        }
        return frontfont;
    }
    private static FrontFont singleton = null;

    private FrontFont()
    {
    }

    public static FrontFont getInstance()
    {
        if(singleton==null)
        {
            synchronized(FrontFont.class)
            {
                if(singleton==null)
                {
                    singleton = new FrontFont();
                }
            }
        }
        return singleton;
    }
}
